package cn.vc.shop.controller;

import cn.vc.auth.entity.po.VUser;
import cn.vc.basicmodule.entity.Result;
import cn.vc.shop.entity.dto.ShoppingCartDto;
import cn.vc.shop.service.ShoppingCartService;
import cn.vc.shop.util.SecurityUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 购物车接口
 */
@RestController
@RequestMapping("/shopping_cart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 添加商品
     * @param commodityId 商品ID
     * @param num 商品数量
     * @return
     */
    @PostMapping
    public Result<?> addCommodity(@RequestParam Long commodityId, @RequestParam Integer num) {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        boolean bo = shoppingCartService.insert(myUser.getId(), commodityId, num);
        return new Result<>(bo);
    }



    /**
     * 查询我的购物车
     * @return
     */
    @GetMapping
    public Result<List<ShoppingCartDto>> selectMyCart() {
        VUser myUser = SecurityUtil.getUser();
        if (myUser==null) return Result.NOT_LOGGED_IN();
        List<ShoppingCartDto> shoppingCartDtoList = shoppingCartService.selectCartByUserId(myUser.getId());
        return new Result<>(1, shoppingCartDtoList);
    }

}
